package task.manager;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class CategoryList implements Serializable {
	
	private static final long serialVersionUID = 1;
	private ArrayList<Category> categories;
	
	private final int ALL = 0;
	private final int COMPLETED = 1;
	private final int CATEGORY = 2;
	private final int DATE = 3;
	
	public CategoryList(){
		categories = new ArrayList<Category>();
	}
	
	public void add(Category c){
		System.out.println("Category List: " + categories);
		categories.add(c);
		System.out.println("Category List: " + categories + " " + categories.get(0).getName());
	}
	
	public void delete(Category c){
		categories.remove(c);
	}
	
	public void addTask(Category c, Task t){
		Category cat = null;
		
		for(int i = 0; i<categories.size();i++){
			//System.out.println("FIND THE NULL : " + categories + " " + categories.get(i) + " " + c);
			if(categories.get(i).getName().equals(c.getName())){
				cat = categories.get(i);
			}
		}
		System.out.println(cat);
		if(cat==null){
			
			categories.add(c);
			addTask(c,t);
		}
		else{
			System.out.println("2 " + cat);
			cat.addTask(t);
		}
	}
	
	public String[] getNames(int state,int month,int day,int year){
		ArrayList<String> names = new ArrayList<String>();
		Category currentCat = null;
		Task[] tasks;
		
		if(state == ALL){
			System.out.println("Categories.size: " +categories.size());
			for(int i=0; i<categories.size(); i++){
				currentCat = categories.get(i);
				tasks = currentCat.getTaskArray();
				System.out.println("tasks.length:" + tasks.length);
				for(int index = 0;index<tasks.length;index++){
					System.out.println("task: " + tasks[index].getTitle());
				}
				for(int j = 0;j<tasks.length;j++){
					System.out.println("Adding " + tasks[j].getTitle() + "to names");
					names.add(tasks[j].getTitle());
				}
			}
		}
		else if(state == COMPLETED){
			for(int i=0; i<categories.size(); i++){
				currentCat = categories.get(i);
				tasks = currentCat.getTaskArray();
				for(int j = 0;j<tasks.length;j++){
					if(!tasks[j].getIsCompleted()){
						names.add(tasks[j].getTitle());
					}
				}
			}
		}
		else if(state == CATEGORY){
			for(int i=0; i<categories.size(); i++){
				names.add(categories.get(i).getName());
			}
		}
		else if(state == DATE){
			for(int i=0; i<categories.size(); i++){
				currentCat = categories.get(i);
				tasks = currentCat.getTaskArray();
				for(int j = 0;j<tasks.length;j++){
					if(tasks[j].isAfter(month,day,year)){
						names.add(tasks[j].getTitle());
					}
				}
			}
		}
		
		String[] tempArray = new String[names.size()];
		names.toArray(tempArray);
		return tempArray;
	}
	
	public Category getCategory(String name){
		for(int i=0; i<categories.size(); i++){
			if (categories.get(i).getName().equals(name)){
				return categories.get(i);
			}
		}
		return null;
	}
	
	public List getCategoryArray(){
		return categories;
	}

	public TaskList getTasksFromCategory(String selectedCat) {
		for(int i=0; i<categories.size(); i++){
			if (categories.get(i).getName().equals(selectedCat)){
				return categories.get(i).getTaskList();
			}
		}
		return null;
	}

	public boolean contains(Category c) {
		return categories.contains(c);
	}

	public Category getCategoryOfTask(String task) {
		Category retCat = null;
		Category currentCat = null;
		for(int i = 0;i<categories.size();i++){
			currentCat = categories.get(i);
			if(currentCat.getTaskList().getTask(task)!=null){
				retCat = currentCat;
			}
		}
		
		return retCat;
	}

}
